package com.zhentao.config;


import com.zhentao.util.JwtService;
import com.zhentao.util.ThreadLocalUtil;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

public class Token implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String token = request.getHeader("token");

        int i = JwtService.verifyToken(token);
        if(i!=1){
            return false;
        }else {
            Map<String, Object> map = JwtService.getClaimsMap(token);
            Object id = map.get("id");
            Object name = map.get("name");

            Map<String,Object>map1=new HashMap<>();
            map1.put("id",id);
            map1.put("name",name);
            System.err.println(map1+"token包含的id和name");

            ThreadLocalUtil.set(map1);

            return true;
        }
    }


}
